package mygang.controller.manager;

import javax.servlet.http.HttpServletRequest;
import mygang.controller.AbstractController;
import mygang.model.constructor.field.Element;
import mygang.service.manager.ElementService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 *
 * @author Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
 * date: 25/02/2013
 */
@Controller
@RequestMapping(AbstractController.MANAGE_ELEMENT)
public class ElementController extends AbstractManageController<ElementService, Element> {

    private static final Logger log = Logger.getLogger(ElementController.class.toString());

    Element prepareObject(HttpServletRequest request) throws Exception {
        String tempId = request.getParameter("id");
        Long id = tempId!=null && !tempId.isEmpty() ? new Long(request.getParameter("id")) : null;
        String tag = request.getParameter("tag");
        if(tag==null || tag.isEmpty()) throw new Exception("Tag is not specified");
        String type = request.getParameter("type");
        return id==null ? new Element(tag, type) : new Element(id.longValue(), tag, type) ;
    }

    String getLink() {
        return MANAGE_ELEMENT;
    }
}